package com.qf.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.qf.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @Author：刘亚康
 * @Date：2020/4/12
 * @QQ：50770284
 * @Phone：18235947935
 * @Desc : 支付类
 **/
@Controller
@Slf4j
public class PayController {
    @Reference
    private IOrderService orderService;

    @RequestMapping("pay")
    public void aLiPay(HttpServletResponse httpResponse,String orderId,String subtotal ) throws IOException {
        //AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipay.com/gateway.do" , APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);  //获得初始化的AlipayClient
        AlipayClient alipayClient =  getAlipayClient();
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
        alipayRequest.setReturnUrl( "http://www.baidu.com" );
        alipayRequest.setNotifyUrl( "http://ptmb9w.natappfree.cc/uploadOrderStatus" ); //在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent( "{"  +
                "    \"out_trade_no\":\""+orderId+"\","  +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
                "    \"total_amount\":"+subtotal+","  +
                "    \"subject\":\"Iphone6 16G\","  +
                "    \"body\":\"Iphone6 16G\","  +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
                "    \"extend_params\":{"  +
                "    \"sys_service_provider_id\":\"2088511833207846\""  +
                "    }" +
                "  }" ); //填充业务参数

        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType( "text/html;charset="  + "utf-8");
        httpResponse.getWriter().write(form); //直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

    public static AlipayClient getAlipayClient() {
        ResourceBundle bundle = ResourceBundle.getBundle("alipay_openapi_sanbox");
        // 网关
        String URL = bundle.getString("ALIPAY_GATEWAY_URL");
        // 商户APP_ID
        String APP_ID = bundle.getString("APP_ID");
        // 商户RSA 私钥
        String APP_PRIVATE_KEY = bundle.getString("RSA2_PRIVATE_KEY");
        // 请求方式 json
        String FORMAT = bundle.getString("FORMAT");
        // 编码格式，目前只支持UTF-8
        String CHARSET = bundle.getString("CHARSET");
        // 支付宝公钥
        String ALIPAY_PUBLIC_KEY = bundle.getString("ALIPAY_RSA2_PUBLIC_KEY");
        // 签名方式
        String SIGN_TYPE = bundle.getString("SIGN_TYPE");
        return new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
    }
    @RequestMapping("uploadOrderStatus")
    public String uploadOrderStatus(String out_trade_no,String trade_status){
        System.out.println("订单为："+out_trade_no);
        System.out.println("订单为："+trade_status);
        if (trade_status.equals("TRADE_SUCCESS")) {
            System.out.println("支付成功");
            log.info("支付成功，更改订单状态");
            int successStatus=1;
            int result=orderService.updateOrderStatus(out_trade_no,successStatus);
            if (result>0) {
                return "ok";
            }else {
                return "error";
            }
        }else {
            return "ok";
        }
    }
}
